<template>
  <base-breadcrumb :title="$route.meta.title">
    <!-- BIM成果列表编辑 -->
    <a-form-model
      id="base-breadcrumb"
      ref="ruleForm"
      :model="form"
      :label-col="{ span: 6 }"
      :wrapper-col="{ span: 18 }"
      :rules="editModel != 0 ? rules : {}"
      style="position: relative"
    >
      <a-spin :spinning="loading">
        <!-- 基本信息 -->
        <a-card :bordered="false" style="margin-bottom: 50px">
          <title-name title="基本信息" />
          <custom-form
            :is-emit="false"
            :form-list="formList"
            :form="form"
            :edit-model="editModel == 0 ? 'V' : ''"
            :form-components="$refs"
          />
          <title-name title="附件资料" />
          <UploadTableNew
            class="mb20"
            :disabled="editModel == 0"
            :init-file-list="form.fileInfoList"
            business-code="BIM05"
          />
          <!-- 页脚操作栏 -->
          <div class="btn_box page-btn-right-top">
            <a-button
              v-if="editModel != 0"
              :loading="loading"
              type="primary"
              class="button_right"
              @click="handleSubmit"
            >
              提交
            </a-button>
            <a-button v-if="checkVisibleInDetail" type="primary" @click="handleActiveClose"> 关闭 </a-button>
          </div>
        </a-card>
      </a-spin>
    </a-form-model>
  </base-breadcrumb>
</template>

<script>
import { insertBimResult, updateBimResult, getBimResultById } from '@/api/bimIntegrate/bimResult.js'
import { getPageTypesLabel } from '@/api/bimIntegrate/bimResource.js'
import BaseEditPage from '@/views/baseEditPage' // 编辑详情页基础配置
import SelectCompanyAndProject from '@/components/Select/SelectCompanyAndProject'
import Selector from '@/components/Select/Selector'

export default {
  name: 'a' + Date.now(),
  extends: new BaseEditPage(),
  data() {
    return {
      loading: false, // 草稿和提交按钮loding
      editModel: 0, // 操作状态:0 查看;1 编辑;2 新增
      id: '', // 业务id
      form: {
        unitId: '', // 单位id
        unitName: '', // 单位
        fileInfoList: [] // 文件列表
      }, // 表单
      rules: {
        resultName: [{ required: true, whitespace: true, message: '请输入成果名称', trigger: 'change' }],
        finishDate: [{ required: true, message: '请选择完成日期', trigger: 'change' }],
        type: [{ required: true, message: '请选择类别', trigger: 'change' }],
        resultProducer: [{ required: true, whitespace: true, message: '请输入成果制作者', trigger: 'change' }],
        unitId: [{ required: true, message: '请选择成果所有者', trigger: 'change' }],
        resultLevel: [{ required: true, message: '请选择成果等级', trigger: 'change' }]
      }
    }
  },
  mounted() {
    this.$route.meta.title = ['成果查看', '成果编辑', '成果新增'][this.editModel]
    // 不为添加时请求详情
    if (this.editModel != 2) {
      this.loading = true
      getBimResultById({
        id: this.id
      })
        .then((res) => {
          this.loading = false
          !res.data.fileInfoList && (res.data.fileInfoList = [])
          this.form = res.data
        })
        .catch(() => {
          this.loading = false
        })
    } else {
      this.form.unitName = this.$currentPost()?.name // 默认单位
      this.form.unitId = this.$currentPost()?.id // 默认单位id
      this.form.unitCode = this.$currentPost()?.code // 默认单位id
    }
  },
  computed: {
    // 表单列表
    formList() {
      return [
        {
          keyName: 'resultName',
          type: 'BaseInput',
          label: '成果名称'
        },
        {
          keyName: 'type',
          viewName: 'typeName',
          type: Selector,
          label: '类别',
          attrs: {
            customApi: getPageTypesLabel,
            customQuery: { param: { type: '31' } },
            dataValue: 'data.list'
          }
        },
        {
          keyName: 'unitId',
          viewName: 'unitName',
          type: SelectCompanyAndProject,
          label: '成果所有者',
          onSelectChange: (e) => {
            this.form.unitName = e?.name
            this.form.unitCode = e?.code
          }
        },
        {
          keyName: 'resultProducer',
          type: 'SelectPerson',
          isEcho: true,
          label: '成果制作人',
          personKeys: ['resultProducerId', 'resultProducer'],
          span: 24
        },
        {
          keyName: 'finishDate',
          type: 'a-date-picker',
          label: '完成日期',
          attrs: {
            valueFormat: 'YYYY-MM-DD'
          }
        },
        {
          keyName: 'resultLevel',
          label: '成果评价',
          placeholder:'仅编辑时可用',
          disabled: this.editModel !== 1,
          type: 'a-select',
          data: [
            {
              label: '优秀',
              value: 0
            },
            {
              label: '一般',
              value: 1
            },
            {
              label: '差',
              value: 2
            }
          ],
          customRender(e) {
            return ['优秀', '一般', '差'][e] || '无'
          }
        },
        {
          keyName: 'remark',
          type: 'BaseInput',
          label: '备注'
        }
      ]
    }
  },
  methods: {
    /**
     * 提交表单
     */
    handleSubmit() {
      let that = this
      let url = [updateBimResult, insertBimResult][that.editModel - 1] // 根据操作切换url地址
      // 校验表单
      that.$refs.ruleForm.validate((e) => {
        if (e) {
          // 提交表单
          that.loading = true
          let form = that.$clone(that.form)
          url(form)
            .then((res) => {
              that.loading = false
              if (res.code === '0' || res.data.code === '0') {
                that.$message.success('提交成功！')
                that.$multiTab.close()
              }
            })
            .catch((res) => {
              that.loading = false
            })
        }
      })
    }
  }
}
</script>